<template>
  <div class="app-container customer-div">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="按商品询价" name="first">
        <div style="margin-top: 20px">
          <!--搜索-->
          <el-form :inline="true" :model="listQuery" size="mini" label-width="140px">
            <el-form-item label="牌号：">
              <el-select
                      @change="getListData"
                      clearable
                      v-model="listQuery.goodsKingId"
                      filterable
                      remote
                      reserve-keyword
                      placeholder="请输入关键词"
                      :remote-method="remoteMethod"
                      :loading="loading">
                <el-option
                        v-for="item in options"
                        :key="item.kingId"
                        :label="item.goodsSn"
                        :value="item.kingId">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="询价时间：">
              <el-date-picker
                      value-format="yyyy-MM-dd HH:mm"
                      v-model="listQuery.createTime"
                      type="datetime"
                      placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-form>
        </div>
        <!--数据列表-->
        <div class="table-container">
          <el-form>
            <div style="background: #f5f7fa;padding:10px;font-size:12px;color:#909399;border:1px solid #EBEEF5;border-bottom:0">
              <el-input v-model="value" size="mini" style="width:150px;" placeholder="请输入供应商名称" clearable></el-input>
              <el-button size="mini" type="primary" @click="saixun">筛选</el-button>
            </div>
            <el-table
                    v-loading="listLoading"
                    :data="list"
                    border
                    style="width: 100%;">
              <el-table-column
                      label="供应商"
                      width="300">
                <template slot-scope="scope">
                  <el-link @click="toDetail(scope.row)" type="primary">
                    {{scope.row.supplierName}}
                  </el-link>
                </template>
              </el-table-column>
              <el-table-column
                      prop="supplierType"
                      label="类型"
                      align="center"
                      width="100">
              </el-table-column>
              <el-table-column
                      prop="contactName"
                      label="联系人"
                      align="center"
                      width="100">
              </el-table-column>
              <el-table-column
                      align="center"
                      prop="chargeGoodsList"
                      label="数量">
                <template slot-scope="scope">
                  <el-form-item>
                    <el-input size="small" v-model="scope.row.goodsNum"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                      align="center"
                      prop="chargeGoodsList"
                      label="散货单价">
                <template slot-scope="scope">
                  <el-form-item>
                    <el-input size="small" v-model="scope.row.goodsPrice"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                      align="center"
                      prop="chargeGoodsList"
                      label="整车单价">
                <template slot-scope="scope">
                  <el-form-item>
                    <el-input size="small" v-model="scope.row.batchPrice"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                      align="center"
                      prop="chargeGoodsList"
                      label="交货期">
                <template slot-scope="scope">
                  <el-form-item>
                    <el-input size="small" v-model="scope.row.deliveryDate"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                      align="center"
                      prop="chargeGoodsList"
                      label="交货地">
                <template slot-scope="scope">
                  <el-form-item>
                    <el-input size="small" v-model="scope.row.deliveryArea"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>

              <el-table-column
                      align="center"
                      prop="chargeGoodsList"
                      label="备注">
                <template slot-scope="scope">
                  <el-form-item>
                    <el-input size="small" v-model="scope.row.remark"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
            </el-table>
          </el-form>

        </div>
        <div style="margin-top:20px;text-align: center;">
          <el-button type="primary" @click="handleConfirm" :loading="btnLoading" size="small">保存</el-button>
        </div>
      </el-tab-pane>
      <el-tab-pane label="按供应商询价" name="second">
        <div style="margin-top: 20px">
          <!--搜索-->
          <el-form :inline="true" :model="listQuery2" size="mini" label-width="140px">
            <el-form-item label="供应商名称：">
              <el-select
                      clearable
                      @change="getListData2"
                      v-model="listQuery2.supplierId"
                      filterable
                      remote
                      reserve-keyword
                      placeholder="请输入关键词"
                      :remote-method="remoteMethod3"
                      :loading="loading2">
                <el-option
                        v-for="item in supplierList"
                        :key="item.supplierId"
                        :label="item.supplierName"
                        :value="item.supplierId">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="询价时间：">
              <el-date-picker
                      value-format="yyyy-MM-dd HH:mm"
                      v-model="listQuery2.createTime"
                      type="datetime"
                      placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-form>
        </div>
        <!--数据列表-->
        <div class="table-container">
          <el-form>
            <el-table
                    v-loading="listLoading2"
                    :data="list2"
                    border
                    style="width: 100%; margin-top: 20px">
              <el-table-column
                      label="牌号"
                      align="center">
                <template slot-scope="scope">
                  {{scope.row.goodsSn}}
                </template>
              </el-table-column>
              <el-table-column
                      align="center"
                      prop="chargeGoodsList"
                      label="数量">
                <template slot-scope="scope">
                  <el-form-item>
                    <el-input size="small" v-model="scope.row.goodsNum"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                      align="center"
                      prop="chargeGoodsList"
                      label="散货单价">
                <template slot-scope="scope">
                  <el-form-item>
                    <el-input size="small" v-model="scope.row.goodsPrice"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                      align="center"
                      prop="chargeGoodsList"
                      label="整车单价">
                <template slot-scope="scope">
                  <el-form-item>
                    <el-input size="small" v-model="scope.row.batchPrice"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                      align="center"
                      prop="chargeGoodsList"
                      label="交货期">
                <template slot-scope="scope">
                  <el-form-item>
                    <el-input size="small" v-model="scope.row.deliveryDate"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                      align="center"
                      prop="chargeGoodsList"
                      label="交货地">
                <template slot-scope="scope">
                  <el-form-item>
                    <el-input size="small" v-model="scope.row.deliveryArea"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                      align="center"
                      prop="chargeGoodsList"
                      label="备注">
                <template slot-scope="scope">
                  <el-form-item>
                    <el-input size="small" v-model="scope.row.remark"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
            </el-table>
          </el-form>

        </div>
        <div style="margin-top:20px;text-align: center;">
          <el-button type="primary" @click="handleConfirm2" :loading="btnLoading2" size="small">保存</el-button>
        </div>
      </el-tab-pane>
    </el-tabs>


  </div>
</template>

<script>
  import {supplierListApi,goodsList,supplierInquiryAdd,supplierGoodsList} from '@/api/company'
  import {formatDate} from '@/utils/date';
  import store from '@/store'
  import { mapGetters } from 'vuex'
  const defaultListQuery={
    createTime:'',
    goodsKingId:''
  };
  const defaultListQuery2={
    createTime:'',
    supplierId:''
  };
  export default {
    name: "index",
    data(){
      return{
        roles:store.getters.roles ? store.getters.roles : [],
        listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
        list:[],
        listLoading:false,
        loading:false,
        options:[],
        btnLoading:false,
        filtersList:[],
        value:'',
        activeName:'first',

        listQuery2: JSON.parse(JSON.stringify(defaultListQuery2)),
        list2:[],
        listLoading2:false,
        loading2:false,
        btnLoading2:false,
        supplierList:[],
        supplierId:null,
        supplierName:''

      }
    },
    computed: {
      ...mapGetters([
        'name',
        'mobile',
        'userId'
      ]),
    },
    created() {
      this.listQuery.createTime = formatDate(new Date(), 'yyyy-MM-dd hh:mm');
      this.listQuery2.createTime = formatDate(new Date(), 'yyyy-MM-dd hh:mm');
    },
    filters: {
      formatTime(time) {
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd')
      },
    },
    methods:{
      //提交询价记录
      handleConfirm2(){
        let data = {};
        data.inquiryDate = this.listQuery2.createTime;
        data.inquiryData = this.list2;
        this.btnLoading = true;
        supplierInquiryAdd(data).then(()=>{
          this.btnLoading2 = false;
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.$router.push({name:'supplierInquiry',query:{tab:this.$route.query.tab}})
        }).catch(()=>{
          this.btnLoading2 = false;
        })
      },
      //获取供应商列表
      remoteMethod3(query) {
        if(query && query!= ''){
          this.loading2 = true;
          //获取供应产列表
          supplierListApi({tab:this.$route.query.tab,supplierName:query,pageNum:1,pageSize:1000}).then(response => {
            this.supplierList = response.list;
            this.loading2 = false;
          }).catch(()=>{
            this.loading2 = false;
          });

        }
      },

      //获取列表
      getListData2(){
        if(this.listQuery2.supplierId && this.listQuery2.supplierId != ''){
          this.listLoading2 = true;
          supplierGoodsList({supplierId:this.listQuery2.supplierId,tab:'all',pageNum:1,pageSize:1000}).then((res)=>{
            this.list2 = [];
            res.list.forEach((el,index)=>{
              let c = {supplierId:el.supplierId,goodsSn:el.goodsSn,goodsKingId:el.goodsKingId,goodsNum:'',deliveryDate:'',deliveryArea:'',goodsPrice:'',batchPrice:'',remark:''}
              this.list2.push(c)
            })
            this.listLoading2 = false;
          }).catch(()=>{
            this.listLoading2 = false;
          })
        }else{
          this.list2 = [];
        }

      },
      //切换询价类型
      handleClick(){

      },
      saixun(){
        if(this.value && this.value != ''){
            this.list = this.filtersList.filter((el)=>{
              return el.supplierName.indexOf(this.value) != -1
            })
        }else{
          this.list = JSON.parse(JSON.stringify(this.filtersList))
        }
      },
      //提交询价记录
      handleConfirm(){
          let data = {};
          data.inquiryDate = this.listQuery.createTime;
          data.inquiryData = this.list;
          this.btnLoading = true;
          supplierInquiryAdd(data).then(()=>{
            this.btnLoading = false;
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.$router.push({name:'supplierInquiry',query:{tab:this.$route.query.tab}})
          }).catch(()=>{
            this.btnLoading = false;
          })
      },

      //获取商品列表
      remoteMethod(query) {
        if (query !== '') {
          this.loading = true;
          this.loading = false;
          goodsList({keywords:query}).then((res)=>{
            this.options = res.list;
          }).catch(()=>{
            this.loading = false;
          });
        } else {
          this.options = [];
        }
      },

      //去详情页
      toDetail(item){
        let routeData = this.$router.resolve({name:'SupplierInquiryInfo',query:{id:item.supplierId,name:item.supplierName}});
        window.open(routeData.href);
      },

      //获取列表
      getListData(){
        if(this.listQuery.goodsKingId && this.listQuery.goodsKingId != ''){
          this.listLoading = true;
          supplierListApi({goodsKingId:this.listQuery.goodsKingId,tab:this.$route.query.tab,pageNum:1,pageSize:1000}).then((res)=>{
            this.list = [];
            this.filtersList = [];
            res.list.forEach((el,index)=>{
              let c = {goodsKingId:this.listQuery.goodsKingId,supplierName:el.supplierName,supplierId:el.supplierId,contactName:el.contactName,contactId:el.contactId,supplierType:el.supplierType,goodsNum:'',deliveryDate:'',deliveryArea:'',goodsPrice:'',batchPrice:'',aboutPrice:'',remark:''}
              this.list.push(c)
              this.filtersList.push(c)
            })
            this.listLoading = false;
          }).catch(()=>{
            this.listLoading = false;
          })
        }else{
          this.list = [];
          this.filtersList = [];
        }

      },
    },
  }
</script>

<style scoped lang="scss">
  .avatar-uploader{
    margin-bottom:10px;
    display: inline-block;
    width:80px;
    height:80px;
    border:1px solid #DCDFE6;
    text-align: center;
    line-height: 80px;
  }
</style>
